

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 以往课设记录  Web期末课程设计v1.0（一） | 大学生闲置物品交易系统 | 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆  Web期末课程设计v1.0（二） | 闲优购 | 使用Bootstrap5各组件优化页面，使用AJAX请求实现用户修改信息功能  Web期末课程设计v1.0（三） | 闲优购系统 | 基于Bootstrap设计商品页面、使用JQuer">
<meta property="og:type" content="article">
<meta property="og:title" content="Web项目实战 _ 购物系统v1.0 _ 开发记录（五） _ 模拟网站埋点实现用户浏览记录的统计与显示">
<meta property="og:url" content="http://example.com/2021/12/24/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E4%BA%94%EF%BC%89%20_%20%E6%A8%A1%E6%8B%9F%E7%BD%91%E7%AB%99%E5%9F%8B%E7%82%B9%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E6%B5%8F%E8%A7%88%E8%AE%B0%E5%BD%95%E7%9A%84%E7%BB%9F%E8%AE%A1%E4%B8%8E%E6%98%BE%E7%A4%BA/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 以往课设记录  Web期末课程设计v1.0（一） | 大学生闲置物品交易系统 | 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆  Web期末课程设计v1.0（二） | 闲优购 | 使用Bootstrap5各组件优化页面，使用AJAX请求实现用户修改信息功能  Web期末课程设计v1.0（三） | 闲优购系统 | 基于Bootstrap设计商品页面、使用JQuer">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6e5551f9d4d846108a576632752ebcf9.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/195badfa89ae49dfa4cb4426a9f1d4ac.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/76a70bd0a5d7482bbeae7cf3cb2b9e46.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9e261040923347b48d947e42e93a1055.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/efb14dadb4bc48d198497a2d6d2fadda.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2ea5b42a201343f1823d6face74a9301.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/eecd961ad11b438d9989374951f1db08.png">
<meta property="article:published_time" content="2021-12-24T03:14:10.000Z">
<meta property="article:modified_time" content="2022-08-22T15:50:32.566Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="JavaWeb">
<meta property="article:tag" content="项目实战:购物系统v1.0">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/6e5551f9d4d846108a576632752ebcf9.png#pic_center">
  
  
  
  <title>Web项目实战 _ 购物系统v1.0 _ 开发记录（五） _ 模拟网站埋点实现用户浏览记录的统计与显示 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Web项目实战 _ 购物系统v1.0 _ 开发记录（五） _ 模拟网站埋点实现用户浏览记录的统计与显示"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-12-24 11:14" pubdate>
          2021年12月24日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          10k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          87 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Web项目实战 _ 购物系统v1.0 _ 开发记录（五） _ 模拟网站埋点实现用户浏览记录的统计与显示</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="以往课设记录"><a href="#以往课设记录" class="headerlink" title="以往课设记录"></a>以往课设记录</h1><hr>
<ul>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122028999">Web期末课程设计v1.0（一） | 大学生闲置物品交易系统 | 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122035714">Web期末课程设计v1.0（二） | 闲优购 | 使用Bootstrap5各组件优化页面，使用AJAX请求实现用户修改信息功能</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122055272">Web期末课程设计v1.0（三） | 闲优购系统 | 基于Bootstrap设计商品页面、使用JQuery实现上传文件并显示到图片组件、使用JSTL标签显示数据库的内容到无序列表</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122098853">Web期末课程设计v1.0（四） | 闲优购系统 | 实现JQuery图片上传 | 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 | 商品分类分页显示</a></p>
</li>
</ul>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>IDEA专业版</li>
<li>JDK8</li>
<li>HTML5、CSS3、JavaScript</li>
<li>JQuery</li>
<li>Bootstrap5</li>
</ul>
<h1 id="1-页面效果展示"><a href="#1-页面效果展示" class="headerlink" title="1. 页面效果展示"></a>1. 页面效果展示</h1><hr>
<h2 id="1-1-显示当前位置"><a href="#1-1-显示当前位置" class="headerlink" title="1.1 显示当前位置"></a>1.1 显示当前位置</h2><p><img src="https://img-blog.csdnimg.cn/6e5551f9d4d846108a576632752ebcf9.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="1-2-购买商品时候的询问窗口"><a href="#1-2-购买商品时候的询问窗口" class="headerlink" title="1.2 购买商品时候的询问窗口"></a>1.2 购买商品时候的询问窗口</h2><p><img src="https://img-blog.csdnimg.cn/195badfa89ae49dfa4cb4426a9f1d4ac.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>确认购买后</p>
<p><img src="https://img-blog.csdnimg.cn/76a70bd0a5d7482bbeae7cf3cb2b9e46.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="1-3-显示浏览记录"><a href="#1-3-显示浏览记录" class="headerlink" title="1.3 显示浏览记录"></a>1.3 显示浏览记录</h2><p><img src="https://img-blog.csdnimg.cn/9e261040923347b48d947e42e93a1055.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="2-功能实现"><a href="#2-功能实现" class="headerlink" title="2. 功能实现"></a>2. 功能实现</h1><h2 id="2-1-使用Boostrap5框架的Modal实现确认购买后的弹出的窗口"><a href="#2-1-使用Boostrap5框架的Modal实现确认购买后的弹出的窗口" class="headerlink" title="2.1 使用Boostrap5框架的Modal实现确认购买后的弹出的窗口"></a>2.1 使用Boostrap5框架的Modal实现确认购买后的弹出的窗口</h2><p><a target="_blank" rel="noopener" href="https://v5.bootcss.com/docs/5.1/components/modal/">Bootstrap5 modal 官方文档</a><br><img src="https://img-blog.csdnimg.cn/efb14dadb4bc48d198497a2d6d2fadda.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>关于modal组件的使用：</p>
<ul>
<li>一个button按钮组件负责启动一个modal模态框<br>在按钮中需要设置这几个参数</li>
</ul>
<table>
<thead>
<tr>
<th>参数名称</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>data-bs-toggle&#x3D;modal</td>
<td>用于指定按钮的目标为一个modal模态框</td>
</tr>
<tr>
<td>data-bs-target &#x3D; #…</td>
<td>用于指定当前按钮绑定的模态框组件的ID</td>
</tr>
</tbody></table>
<ul>
<li>一个div负责显示modal模态框的内容，模态框所需的参数参考官方代码，除了id外，笔者未作修改</li>
</ul>
<p><code>jsp</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- Button trigger modal --&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;bt-buy&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">&quot;modal&quot;</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">&quot;#staticBackdrop&quot;</span> <span class="hljs-attr">hidden</span>&gt;</span><br>    modal组件<br><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><span class="hljs-comment">&lt;!-- Modal --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal fade&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;staticBackdrop&quot;</span> <span class="hljs-attr">data-bs-backdrop</span>=<span class="hljs-string">&quot;static&quot;</span> <span class="hljs-attr">data-bs-keyboard</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">tabindex</span>=<span class="hljs-string">&quot;-1&quot;</span> <span class="hljs-attr">aria-labelledby</span>=<span class="hljs-string">&quot;staticBackdropLabel&quot;</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">&quot;true&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-dialog&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-content&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-header&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-title text-danger&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;staticBackdropLabel&quot;</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;bi bi-exclamation-triangle-fill&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><br>                    温馨提示<br>                <span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn-close&quot;</span> <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">&quot;modal&quot;</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">&quot;Close&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-body&quot;</span>&gt;</span><br>                是否确认购买？<br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-footer&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;btn-buy-cancel&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger&quot;</span> <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">&quot;modal&quot;</span>&gt;</span>我再想想<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;btn-buy-ok&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span> <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">&quot;modal&quot;</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">&quot;#modal-buy-ok&quot;</span>&gt;</span>确认购买<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- Modal --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal fade&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;modal-buy-ok&quot;</span> <span class="hljs-attr">tabindex</span>=<span class="hljs-string">&quot;-1&quot;</span> <span class="hljs-attr">aria-labelledby</span>=<span class="hljs-string">&quot;modal-h-buy-ok&quot;</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">&quot;true&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-dialog&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-content&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-header&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-title text-danger&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;modal-h-buy-ok&quot;</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;bi bi-exclamation-triangle-fill&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><br>                    温馨提示<br>                <span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn-close&quot;</span> <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">&quot;modal&quot;</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">&quot;Close&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-body&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;display-5 text-success text-center&quot;</span>&gt;</span><br>                    购买成功!<br>                    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;bi bi-check-circle&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-2-实现记录浏览记录"><a href="#2-2-实现记录浏览记录" class="headerlink" title="2.2 实现记录浏览记录"></a>2.2 实现记录浏览记录</h2><h3 id="2-2-1-存储浏览记录的表、对象"><a href="#2-2-1-存储浏览记录的表、对象" class="headerlink" title="2.2.1 存储浏览记录的表、对象"></a>2.2.1 存储浏览记录的表、对象</h3><p>关于数据表的问题，首先用户uid、商品cid必须有外键约束，即需要是用户表和商品表里真实存在的数据，其次是需关联uid、cid这两个属性，为实现浏览记录的覆盖，这两个字段不能重复出现，具体实现可以看之后的SQL语句与dao层代码。</p>
<p><code>Mysql数据表</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> user_action(<br>     id <span class="hljs-type">int</span> <span class="hljs-keyword">primary</span> key auto_increment,<br>     uid <span class="hljs-type">int</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">null</span>,<br>     cid <span class="hljs-type">int</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">null</span>,<br>     <span class="hljs-type">time</span> <span class="hljs-type">timestamp</span>(<span class="hljs-number">0</span>) <span class="hljs-keyword">null</span> <span class="hljs-keyword">default</span> <span class="hljs-built_in">current_timestamp</span>,<br>    <span class="hljs-keyword">CONSTRAINT</span> a_cid<br>    <span class="hljs-keyword">FOREIGN</span> KEY (cid) <span class="hljs-keyword">REFERENCES</span> commodity(id),<br>    <span class="hljs-keyword">CONSTRAINT</span> a_uid<br>    <span class="hljs-keyword">FOREIGN</span> KEY (uid) <span class="hljs-keyword">REFERENCES</span> <span class="hljs-keyword">user</span>(id)<br>) <span class="hljs-keyword">default</span> charset<span class="hljs-operator">=</span>utf8;<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> INDEX index_user_action <span class="hljs-keyword">ON</span> user_action (uid, cid);<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/2ea5b42a201343f1823d6face74a9301.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>实体类 <code>CommodityRecord.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.entity;<br><br><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-keyword">import</span> java.sql.Timestamp;<br><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CommodityRecord</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> id;<br>    <span class="hljs-keyword">private</span> User user;<br>    <span class="hljs-keyword">private</span> Commodity commodity;<br>    <span class="hljs-keyword">private</span> Timestamp time;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-2-2-浏览记录存储理念——埋点"><a href="#2-2-2-浏览记录存储理念——埋点" class="headerlink" title="2.2.2 浏览记录存储理念——埋点"></a>2.2.2 浏览记录存储理念——埋点</h3><p>当用户点击一个商品卡片时，会产生一个浏览记录，于是可以通过JS代码给每个商品卡片绑定一个函数。</p>
<p>当卡片被单击时，JS提交AJAX请求到后端，后端对操作的用户、浏览的商品存储到数据库。</p>
<p>那么怎么获取到请求的用户以及商品呢？</p>
<p>笔者采取的措施是：</p>
<ol>
<li><p>在JSP页面显示商品卡片时候，通过设置data-…属性，来指定当前商品的用户id和商品id。</p>
</li>
<li><p>JS代码获取用户id和商品id ，提交到Servlet，后端根据这两个ID查询到对应的数据，从而插入新的浏览记录到数据库。</p>
</li>
</ol>
<p>不过在保存记录时，会遇到一个问题：</p>
<p>如果用户访问的商品记录是已经存在的，那么该如何处理？</p>
<p>为防止出现重复记录，即用户点击同一商品的记录都出现在数据表，这样就可能会产生许多冗余数据，其实可以给浏览记录数据表添加一个字段，用于表示浏览的次数，默认为1，不过为简化课程设计，笔者就直接采用覆盖式，将其访问时间覆盖，实现的SQL语句： </p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> user_action (uid, cid) <span class="hljs-keyword">values</span> (?, ?) <span class="hljs-keyword">on</span> duplicate key <span class="hljs-keyword">update</span> <span class="hljs-type">time</span><span class="hljs-operator">=</span>?&quot;<br></code></pre></td></tr></table></figure>
<h3 id="2-2-3-JS埋点"><a href="#2-2-3-JS埋点" class="headerlink" title="2.2.3 JS埋点"></a>2.2.3 JS埋点</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs javascript">$(<span class="hljs-variable language_">document</span>).<span class="hljs-title function_">ready</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-keyword">var</span> cid = <span class="hljs-number">0</span><br>    <span class="hljs-keyword">var</span> uid = <span class="hljs-number">0</span><br>    $(<span class="hljs-string">&quot;.card-commodity&quot;</span>).<span class="hljs-title function_">click</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br>       cid = $(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">data</span>(<span class="hljs-string">&quot;cid&quot;</span>)<br>       uid = $(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">data</span>(<span class="hljs-string">&quot;uid&quot;</span>)<br>        <span class="hljs-comment">// 埋点记录</span><br>        $.<span class="hljs-title function_">ajax</span>(&#123;<br>            <span class="hljs-attr">url</span>: <span class="hljs-string">&#x27;/visted?cid=&#x27;</span> + cid + <span class="hljs-string">&quot;&amp;uid=&quot;</span> + uid,<br>            <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;GET&quot;</span>,<br>            <span class="hljs-attr">success</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>)&#123;<br>            &#125;<br>        &#125;);<br>           $(<span class="hljs-string">&quot;#bt-buy&quot;</span>).<span class="hljs-title function_">click</span>();<br>        &#125;);<br>    $(<span class="hljs-string">&quot;.btn-commodity-type&quot;</span>).<span class="hljs-title function_">click</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">window</span>.<span class="hljs-property">location</span>.<span class="hljs-property">href</span>=$(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">attr</span>(<span class="hljs-string">&quot;href&quot;</span>);<br>    &#125;);<br>    $(<span class="hljs-string">&quot;#btn-buy-ok&quot;</span>).<span class="hljs-title function_">click</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br><br>        <span class="hljs-comment">// 确认购买后，添加商品订单....</span><br> <br>    &#125;);<br>&#125;);<br></code></pre></td></tr></table></figure>

<h3 id="2-2-4-Servlet-处理埋点请求"><a href="#2-2-4-Servlet-处理埋点请求" class="headerlink" title="2.2.4 Servlet 处理埋点请求"></a>2.2.4 Servlet 处理埋点请求</h3><p><code>Action层</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.action;<br><br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><span class="hljs-keyword">import</span> com.uni.service.Service;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@WebServlet(name = &quot;UserVistedAction&quot;, urlPatterns = &quot;/visted&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserVistedAction</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="hljs-keyword">throws</span> ServletException, IOException &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">cid</span> <span class="hljs-operator">=</span> Integer.parseInt(req.getParameter(<span class="hljs-string">&quot;cid&quot;</span>));<br>        <span class="hljs-type">int</span> <span class="hljs-variable">uid</span> <span class="hljs-operator">=</span> Integer.parseInt(req.getParameter(<span class="hljs-string">&quot;uid&quot;</span>));<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) req.getSession().getAttribute(<span class="hljs-string">&quot;userInfo&quot;</span>);<br>        <span class="hljs-comment">// 当用户访问非自己发布商品时，进行埋点记录</span><br>        <span class="hljs-keyword">if</span>(user.getId() != uid)<br>            Service.commodity.insertAction(user.getId(), cid);<br>        resp.getWriter().write(<span class="hljs-string">&quot;埋点完毕!&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>Dao层</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">insertAction</span><span class="hljs-params">(<span class="hljs-type">int</span> uid, <span class="hljs-type">int</span> cid)</span> &#123;<br>     <span class="hljs-type">Connection</span> <span class="hljs-variable">connection</span> <span class="hljs-operator">=</span> DruidUtil.getConnection();<br>     <span class="hljs-type">String</span> <span class="hljs-variable">sql</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;insert into user_action (uid, cid) values (?, ?) on duplicate key update time=?&quot;</span>;<br>     <span class="hljs-type">boolean</span> <span class="hljs-variable">ok</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>     <span class="hljs-keyword">try</span>&#123;<br>         <span class="hljs-type">PreparedStatement</span> <span class="hljs-variable">pstmt</span> <span class="hljs-operator">=</span> connection.prepareStatement(sql);<br>         pstmt.setInt(<span class="hljs-number">1</span>, uid);<br>         pstmt.setInt(<span class="hljs-number">2</span>, cid);<br>         pstmt.setTimestamp(<span class="hljs-number">3</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Timestamp</span>(System.currentTimeMillis()));<br>         ok = pstmt.execute();<br>         DruidUtil.release(connection, pstmt, <span class="hljs-literal">null</span>);<br>     &#125; <span class="hljs-keyword">catch</span> (SQLException throwables) &#123;<br>         throwables.printStackTrace();<br>     &#125;<br>     <span class="hljs-keyword">return</span> ok;<br> &#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-3-5-JSP页面"><a href="#2-3-5-JSP页面" class="headerlink" title="2.3.5 JSP页面"></a>2.3.5 JSP页面</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">c:forEach</span> <span class="hljs-attr">items</span>=<span class="hljs-string">&quot;$&#123;sessionScope.commodityInfo&#125;&quot;</span> <span class="hljs-attr">var</span>=<span class="hljs-string">&quot;cd&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-3 mb-5&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-commodity&quot;</span> <span class="hljs-attr">data-uid</span>=<span class="hljs-string">&quot;$&#123;cd.uid&#125;&quot;</span> <span class="hljs-attr">data-cid</span>=<span class="hljs-string">&quot;$&#123;cd.id&#125;&quot;</span>&gt;</span><br>              <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;$&#123;cd.img&#125;&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-img-top&quot;</span>&gt;</span><br>              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span><br><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title text-center&quot;</span>&gt;</span>￥$&#123;cd.price&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">h6</span>&gt;</span>商品名称: <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;cd.name&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">h6</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-text&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>商品类型: $&#123;cd.typeName&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-text&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>商品描述: $&#123;cd.info&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>商家: <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>$&#123;cd.userName&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>              <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">c:forEach</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h2 id="2-3-显示浏览记录"><a href="#2-3-显示浏览记录" class="headerlink" title="2.3 显示浏览记录"></a>2.3 显示浏览记录</h2><hr>
<p>在记录用户浏览记录后，用户在个人信息页面可以查询到自己的浏览记录，和记录类似，同样是操作数据表。<br><img src="https://img-blog.csdnimg.cn/eecd961ad11b438d9989374951f1db08.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h3 id="2-3-1-Servlet层"><a href="#2-3-1-Servlet层" class="headerlink" title="2.3.1 Servlet层"></a>2.3.1 Servlet层</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.action;<br><br><span class="hljs-keyword">import</span> com.uni.entity.CommodityRecord;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><span class="hljs-keyword">import</span> com.uni.service.Service;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpSession;<br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-meta">@WebServlet(name =&quot;UserHistoryAction&quot;, urlPatterns = &quot;/history&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserHistoryAction</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span><br>            <span class="hljs-keyword">throws</span> ServletException, IOException &#123;<br>        <span class="hljs-type">HttpSession</span> <span class="hljs-variable">session</span> <span class="hljs-operator">=</span> req.getSession();<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span>(User) session.getAttribute(<span class="hljs-string">&quot;userInfo&quot;</span>);<br>        List&lt;CommodityRecord&gt; commodityRecords = Service.user.queryHistory(user);<br>        session.setAttribute(<span class="hljs-string">&quot;commodityRecords&quot;</span>, commodityRecords);<br>        resp.sendRedirect(<span class="hljs-string">&quot;card_user_info.jsp&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-3-2-JSP页面-JSTL应用"><a href="#2-3-2-JSP页面-JSTL应用" class="headerlink" title="2.3.2 JSP页面 JSTL应用"></a>2.3.2 JSP页面 JSTL应用</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;h-title-black&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;bi bi-clock-history&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><br>	浏览记录 共$&#123;sessionScope.commodityRecords.size()&#125;条<br>	<span class="hljs-tag">&lt;<span class="hljs-name">c:if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;$&#123;not empty sessionScope.historyPageNow &amp;&amp; sessionScope.commodityRecords.size() &gt; 0&#125;&quot;</span>&gt;</span><br>		[$&#123;sessionScope.historyPageNow&#125;,$&#123;sessionScope.historyPageNow+1&#125;] 条<br>	<span class="hljs-tag">&lt;/<span class="hljs-name">c:if</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row row-cols-1 row-cols-md-2 g-4&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">c:set</span> <span class="hljs-attr">var</span>=<span class="hljs-string">&quot;pageNow&quot;</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;page&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;sessionScope.historyPageNow&#125;&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">c:set</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">c:forEach</span> <span class="hljs-attr">items</span>=<span class="hljs-string">&quot;$&#123;sessionScope.commodityRecords&#125;&quot;</span> <span class="hljs-attr">begin</span>=<span class="hljs-string">&quot;$&#123;pageNow&#125;&quot;</span> <span class="hljs-attr">end</span>=<span class="hljs-string">&quot;$&#123;pageNow + 1&#125;&quot;</span> <span class="hljs-attr">var</span>=<span class="hljs-string">&quot;cr&quot;</span> <span class="hljs-attr">varStatus</span>=<span class="hljs-string">&quot;index&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">c:if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;$&#123;index.index &lt;= sessionScope.commodityRecords.size()&#125;&quot;</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-commodity-row&quot;</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-commodity&quot;</span> <span class="hljs-attr">data-uid</span>=<span class="hljs-string">&quot;$&#123;cr.user.id&#125;&quot;</span> <span class="hljs-attr">data-cid</span>=<span class="hljs-string">&quot;$&#123;cr.commodity.id&#125;&quot;</span>&gt;</span><br>					<span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;$&#123;cr.commodity.img&#125;&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-img-top&quot;</span>&gt;</span><br>					<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span><br>						<span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title text-center&quot;</span>&gt;</span>￥$&#123;cr.commodity.price&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>						<span class="hljs-tag">&lt;<span class="hljs-name">h6</span>&gt;</span>商品名称: <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;cr.commodity.name&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span><br>						<span class="hljs-tag">&lt;<span class="hljs-name">h6</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-text&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>商品类型: $&#123;cr.commodity.typeName&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span><br>						<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-text&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>商品描述: $&#123;cr.commodity.info&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>						<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>商家: <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>$&#123;cr.user.name&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>					<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>					<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-footer&quot;</span>&gt;</span><br>						<span class="hljs-tag">&lt;<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-muted&quot;</span>&gt;</span>上次浏览日期:  $&#123;fn:replace(cr.time, &quot;.0&quot;, &quot;&quot;)&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span><br>					<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>				<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>			<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">c:if</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">c:forEach</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="3-总结"><a href="#3-总结" class="headerlink" title="3. 总结"></a>3. 总结</h1><hr>
<p>至此，已完成了浏览信息的记录与显示，接下来的阶段就是商品订单信息的添加，其实在整个过程中，笔者做了许多简化工作，目前发现的最大问题就是dao层冗余代码过多，比如要查询一个商品记录，就需要在dao层接口定义一个新的方法，然后impl类实现，接着service层又要声明一个方法，Impl类调用dao层接口实现类的方法。总的来说比较繁琐…</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98-%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1-0/" class="category-chain-item">项目实战:购物系统v1.0</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaWeb/">#JavaWeb</a>
      
        <a href="/tags/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98-%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1-0/">#项目实战:购物系统v1.0</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Web项目实战 _ 购物系统v1.0 _ 开发记录（五） _ 模拟网站埋点实现用户浏览记录的统计与显示</div>
      <div>http://example.com/2021/12/24/Web项目实战 _ 购物系统v1.0 _ 开发记录（五） _ 模拟网站埋点实现用户浏览记录的统计与显示/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年12月24日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/25/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E5%85%AD%EF%BC%89_%E4%BD%BF%E7%94%A8coockie%E5%AE%9E%E7%8E%B0%E8%AE%B0%E4%BD%8F%E5%AF%86%E7%A0%81%20_%20%E9%9D%9E%E7%99%BB%E9%99%86%E7%94%A8%E6%88%B7%E6%8B%A6%E6%88%AA%20_%20AJAX%E6%96%87%E5%AD%97%E7%99%BB%E9%99%86%E9%AA%8C%E8%AF%81%20_%20%E4%BD%BF%E7%94%A8Bootstrap5%E6%A1%86%E6%9E%B6%E7%9A%84%E6%A8%A1%E6%80%81%E6%A1%86%E7%BB%84%E4%BB%B6%E6%98%BE%E7%A4%BA%E6%8F%90%E7%A4%BA%E5%BC%B9%E7%AA%97/" title="Web项目实战 _ 购物系统v1.0 _ 开发记录（六）_使用coockie实现记住密码 _ 非登陆用户拦截 _ AJAX文字登陆验证 _ 使用Bootstrap5框架的模态框组件显示提示弹窗">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Web项目实战 _ 购物系统v1.0 _ 开发记录（六）_使用coockie实现记住密码 _ 非登陆用户拦截 _ AJAX文字登陆验证 _ 使用Bootstrap5框架的模态框组件显示提示弹窗</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/23/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%BA%94%E7%94%A8%E6%8A%80%E6%9C%AF%20_%20%E4%BD%BF%E7%94%A8%20Docker%E6%90%AD%E5%BB%BA%20Flink%20%E9%9B%86%E7%BE%A4%20_%20FlinkStreaming%E5%AE%9E%E6%97%B6%E6%95%B0%E6%8D%AE%E6%B5%81%E5%A4%84%E7%90%86%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0%E8%AF%8D%E9%A2%91%E7%BB%9F%E8%AE%A1/" title="大数据应用技术_使用 Docker搭建 Flink 集群_FlinkStreaming实时数据流处理程序实现词频统计">
                        <span class="hidden-mobile">大数据应用技术_使用 Docker搭建 Flink 集群_FlinkStreaming实时数据流处理程序实现词频统计</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
